package org.reliablesource.prism.gef.editor.commands;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;
import org.reliablesource.prism.core.plugin.PrismCorePlugin;
import org.reliablesource.prism.gef.editor.model.ScreenAdapter;
import org.reliablesource.prism.gef.editor.model.StickyNoteAdapter;

/**
 * A command to add a PolygonModel to a ScreenAdapter. The command can be undone
 * or redone.
 */
public class StickyNoteCreateCommand extends Command {

	/** ScreenAdapter to add to. */
	private final ScreenAdapter screen;

	private final StickyNoteAdapter stickyNote;

	/**
	 * Create a command that will add a new PolygonModel to a ScreenAdapter.
	 * 
	 * @param stickyNote
	 *            the new PolygonModel that is to be added
	 * @param screen
	 *            the ScreenAdapter that will hold the new element
	 * @throws IllegalArgumentException
	 *             if any parameter is null, or the request does not provide a
	 *             new PolygonModel instance
	 */
	public StickyNoteCreateCommand(final StickyNoteAdapter _newStickyNote,
			final ScreenAdapter _parent, final Rectangle _bounds) {
		screen = _parent;
		if (_newStickyNote != null)
			stickyNote = _newStickyNote;
		else{
			stickyNote = new StickyNoteAdapter(PrismCorePlugin.getDefault()
					.getElementFactory().createPrismAnnotation(
							_parent.getScreen()), _bounds.getLocation().x,
					_bounds.getLocation().y);
		}
		setLabel("Popup Text Box creation");
	}

	/**
	 * Can execute if all the necessary information has been provided.
	 * 
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	public boolean canExecute() {
		return screen != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		redo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	public void redo() {
		screen.addChild(stickyNote);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		screen.removeChild(stickyNote);
	}

}